package com.ruoyi.web.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.web.domain.UserEmpowerConfig;
import com.ruoyi.web.mapper.UserEmpowerConfigMapper;
import com.ruoyi.web.vo.MiningConfigVO;
import com.ruoyi.web.vo.UserEmpowerConfigVO;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.domain.MiningConfig;
import com.ruoyi.web.service.IMiningConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 当前质押配置Controller
 * 
 * @author ruoyi
 * @date 2023-01-04
 */
@RestController
@RequestMapping("/mining/config")
@AllArgsConstructor
public class MiningConfigController extends BaseController
{
    private final IMiningConfigService miningConfigService;
    private final UserEmpowerConfigMapper configMapper;

    /**
     * 获取app-当前质押数据
     * @return r
     */
    @Anonymous
    @GetMapping
    public R<MiningConfigVO> getAppDetail() {
        // 获取质押配置
        List<MiningConfig> list = miningConfigService.selectMiningConfigList(new MiningConfig());
        MiningConfigVO vo = new MiningConfigVO();
        if (list.size() > 0) {
            BeanUtils.copyProperties(list.get(0),vo);
        }
        // 查询挖矿收益级别配置
        List<UserEmpowerConfig> configs = configMapper.selectUserEmpowerConfigList(new UserEmpowerConfig());
        List<UserEmpowerConfigVO> voList = new ArrayList<>();
        for (UserEmpowerConfig config : configs) {
            UserEmpowerConfigVO configVO = new UserEmpowerConfigVO();
            BeanUtils.copyProperties(config,configVO);
            configVO.setMaxReward(configVO.getMaxReward().multiply(new BigDecimal("100")));
            configVO.setMinReward(configVO.getMinReward().multiply(new BigDecimal("100")));
            voList.add(configVO);
        }
        vo.setEmpowerConfig(voList);
        return R.ok(vo);
    }

    /**
     * 查询当前质押配置列表
     */
    @PreAuthorize("@ss.hasPermi('web:config:list')")
    @GetMapping("/list")
    public TableDataInfo list(MiningConfig miningConfig)
    {
        startPage();
        List<MiningConfig> list = miningConfigService.selectMiningConfigList(miningConfig);
        List<MiningConfigVO> voList = new ArrayList<>();
        for (MiningConfig config : list) {
            MiningConfigVO configVO = new MiningConfigVO();
            BeanUtils.copyProperties(config,configVO);
            voList.add(configVO);
        }
        return getDataTable(voList);
    }

    /**
     * 导出当前质押配置列表
     */
    @PreAuthorize("@ss.hasPermi('web:config:export')")
    @Log(title = "当前质押配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MiningConfig miningConfig)
    {
        List<MiningConfig> list = miningConfigService.selectMiningConfigList(miningConfig);
        ExcelUtil<MiningConfig> util = new ExcelUtil<MiningConfig>(MiningConfig.class);
        util.exportExcel(response, list, "当前质押配置数据");
    }

    /**
     * 获取当前质押配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('web:config:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        MiningConfig config = miningConfigService.selectMiningConfigById(id);
        MiningConfigVO configVO = new MiningConfigVO();
        BeanUtils.copyProperties(config,configVO);
        return success(configVO);
    }

    /**
     * 新增当前质押配置
     */
    @PreAuthorize("@ss.hasPermi('web:config:add')")
    @Log(title = "当前质押配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MiningConfig miningConfig)
    {
        return toAjax(miningConfigService.insertMiningConfig(miningConfig));
    }

    /**
     * 修改当前质押配置
     */
    @PreAuthorize("@ss.hasPermi('web:config:edit')")
    @Log(title = "当前质押配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MiningConfig miningConfig)
    {
        return toAjax(miningConfigService.updateMiningConfig(miningConfig));
    }

    /**
     * 删除当前质押配置
     */
    @PreAuthorize("@ss.hasPermi('web:config:remove')")
    @Log(title = "当前质押配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(miningConfigService.deleteMiningConfigByIds(ids));
    }
}
